﻿using AutoMapper;
using MediatR;
using Warehouse.Api.Write.AllCation.Command.Shifing;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Write.AllCation.Hanlder
{
    public class UpdateShiftingCommandHanlder : IRequestHandler<UpdateShiftingCommand, ApiResult<int>>
    {
        public readonly IMapper mapper;
        public readonly IBaseRepostity<Shifting> stockingRepostity;

        public UpdateShiftingCommandHanlder(IMapper mapper, IBaseRepostity<Shifting> stockingRepostity)
        {
            this.mapper = mapper;
            this.stockingRepostity = stockingRepostity;
        }
        /// <summary>
        /// 调拨移库更新的方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(UpdateShiftingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var res = stockingRepostity.GetAll().Where(x => x.Id == request.Id).First();
                var model = mapper.Map(request, res);
                int r = stockingRepostity.UpdateAsync(model).Result;
                if (r > 0)
                {
                    result.Msg = "修改成功";
                    result.Data = r;
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Msg = "修改失败";
                    result.Data = r;
                    result.Code = ApiEume.Error;
                }
            }
            catch (Exception ex)
            {

            }
            return Task.FromResult(result);
        }
    }
}
